package com.rs.service.impl;

import com.rs.dao.RoleDao;
import com.rs.entity.login.Menu;
import com.rs.entity.login.Role;
import com.rs.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;


    @Override
    public List<Role> getRoleList(long page, long limit,String name){

        return roleDao.getRoleList((page-1)*limit,limit,name);
    }

    @Override
    public Long getRoleSum(String name){

        return roleDao.getRoleSum(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long addRole(String name, String remark){
        return roleDao.addRole(name,remark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(long id,String name,String remark){
        roleDao.updateRole(id,name,remark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delRole(long id){
        //删除角色所有权限
        roleDao.deletePower(id);
        //删除角色
        roleDao.deleteRole(id);
    }


    @Override
    public List<Menu> getOneMenu(){

        return roleDao.getOneMenu();
    }


    @Override
    public List<Menu> getTwoMenu(List<Menu> haveMenus){
        List<Menu> twoMenu=roleDao.getTwoMenu();
        for (Menu two:twoMenu){
            for (Menu have:haveMenus){
                if(two.getId()==have.getId()){
                    two.setChecked(true);
                    break;
                }
            }
        }
        return twoMenu;
    }


    @Override
    public List<Menu> getHaveMenu(long id){

        return roleDao.getHaveMenu(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void allot(String mid,int id){
        String[] ids=mid.split(",");
        //删除该角色已有权限
        roleDao.deletePower(id);
        //批量插入勾选的权限
        roleDao.addPower(ids,id);
    }


    @Override
    public List<Role> getAll(){

        return roleDao.getAll();
    }

}
